/**
 * ModuleService.java
 */
package org.yate.oa.service.imp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yate.oa.dao.IBaseMapperDao;
import org.yate.oa.dao.mapper.RoleMapper;
import org.yate.oa.dao.mapper.RoleRefPrivilegeMapper;
import org.yate.oa.model.RoleEntity;
import org.yate.oa.model.RoleRefPrivilegeEntity;
import org.yate.oa.service.IRoleService;

/**
 * @author Yate
 * @date Nov 13, 2013
 * @description TODO
 * @version 1.0
 */
@Service
public class RoleService extends BaseService<RoleEntity, Long> implements
		IRoleService {

	@Resource(name = "roleMapper")
	private RoleMapper roleDao;

	@Resource(name = "roleRefPrivilegeMapper")
	private RoleRefPrivilegeMapper roleRefPrivilegeMapper;
	
	

	/* 
	 * (non-Javadoc)
	 * @see org.yate.oa.service.imp.BaseService#getEntity(java.lang.Object)
	 */
	@Override
	public RoleEntity getEntity(Long id) {
		RoleEntity e = super.getEntity(id);
		e.setRps(this.roleRefPrivilegeMapper.getEntityByRoleId(id));
		return e;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.yate.oa.service.IRoleService#add(java.lang.String,
	 * java.lang.String[])
	 */
	public void add(RoleEntity e, String... privs) {
		roleDao.add(e);

		List<RoleRefPrivilegeEntity> entities = new ArrayList<RoleRefPrivilegeEntity>();
		for (String pid : privs) {
			entities.add(new RoleRefPrivilegeEntity(Long.parseLong(pid), e
					.getRoleId()));
		}
		this.roleRefPrivilegeMapper.batchAdd(entities);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.yate.oa.service.IRoleService#modify(org.yate.oa.model
	 * .RoleEntity, java.lang.String[])
	 */
	@Override
	@Transactional
	public void modify(RoleEntity e, String... privs) {
		this.roleRefPrivilegeMapper.removeByRoleId(e.getRoleId());
		roleDao.update(e);

		List<RoleRefPrivilegeEntity> entities = new ArrayList<RoleRefPrivilegeEntity>();
		for (String pid : privs) {
			entities.add(new RoleRefPrivilegeEntity(Long.parseLong(pid), e
					.getRoleId()));
		}
		this.roleRefPrivilegeMapper.batchAdd(entities);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.yate.oa.service.imp.BaseService#getMapperDao()
	 */
	@Override
	protected IBaseMapperDao<RoleEntity, Long> getMapperDao() {
		return this.roleDao;
	}

}
